
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc, confusion_matrix
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn import preprocessing

# 导入数据集
df = pd.read_csv('E:/PROJECT_Dynasty2023/project_-dynasty2023/Project_MachineLearning/titanic_dataset.csv')

# 数据预处理（填补缺失值代码需要自行填写）
# -------------------------------------
# import numpy as np
# import pandas as pd
# from sklearn.model_selection import train_test_split
# from sklearn.preprocessing import StandardScaler
# from sklearn.svm import SVC
# from sklearn.linear_model import LogisticRegression
# from sklearn.metrics import roc_curve, auc
# import matplotlib.pyplot as plt
# #导入数据集
# df= pd.read_csv('C:/Users/86152/Documents/WPSDrive/1122396675/WPS云盘/550A/机器学习实验/titanic_dataset.csv')
# df = df.dropna(subset=['Embarked'])
# df = df[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
# df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
# df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
# df = df.dropna()

X = df.iloc[:, 1:]
y = df.iloc[:, 0]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
svm = SVC(probability=True)
svm.fit(X_train, y_train)
y_score_svm = svm.predict_proba(X_test)[:, 1]

fpr_svm, tpr_svm, _ = roc_curve(y_test, y_score_svm)
roc_auc_svm = auc(fpr_svm, tpr_svm)

# Logistic Regression
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
y_score_lr = logreg.predict_proba(X_test)[:, 1]

fpr_lr, tpr_lr, _ = roc_curve(y_test, y_score_lr)
roc_auc_lr = auc(fpr_lr, tpr_lr)

# # Plot ROC curve
# plt.figure()
# plt.plot(fpr_svm, tpr_svm, color='darkorange', lw=2, label='SVM (AUC = {:.2f})'.format(roc_auc_svm))
# plt.plot(fpr_lr, tpr_lr, color='green', lw=2, label='Logistic Regression (AUC = {:.2f})'.format(roc_auc_lr))
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('Receiver operating characteristic')
# plt.legend(loc="lower right")
# plt.show()



#导入数据集
features= pd.read_csv('titanic_dataset.csv')

features=features.drop(['Cabin'],axis=1)
features=features.dropna(axis=0, how='any')
print(features)
#把标签和特征分开
y_train1=features['Survived']
X_train=features.drop('Survived',axis=1)
#获得特征的大小
(a,b)=X_train.shape
#获得标签的长度
a1=y_train1.shape
#查看特征集的缺失值情况
# X_train.info()
# 缺失值合计
# X_train.isnull().sum()
# y_train1.isnull().sum()
# 待处理的缺失值
#X_train.Age
# X_train.Cabin
# X_train.Embarked
# X_train.Fare
import seaborn as sns
import matplotlib.pyplot as plt


# 先看下数据集的 Age 分布状态


# 将数据集中的NaN数据使用中值填充。
# Xtrain1=X_train.copy()
#np.nanmedian沿指定轴计算中位数，而忽略NaN。
# Xtrain1['Age'].replace(np.nan, np.nanmedian(Xtrain1['Age']),inplace=True)

#sns.distplot(Xtrain1['Age'].dropna(), hist=True, kde=True)
#性别有关的中位数
age_sex_median=X_train.groupby('Sex').Age.median()
Xtrain2=X_train.set_index('Sex')
Xtrain2.Age.fillna(age_sex_median,inplace=True)
Xtrain2.reset_index(inplace=True)
Xage2=Xtrain2['Age']
#同时考虑性别和仓位
age_Pclass=X_train.groupby(['Pclass','Sex']).Age.median()
X_train.set_index(['Pclass','Sex'],inplace=True) 
X_train.Age.fillna(age_Pclass,inplace=True)
X_train.reset_index(inplace=True)

meanage=[[],[],[]]
meanage[0]=X_train.Age.mean()
# meanage[1]=Xtrain1.Age.mean()
meanage[2]=Xtrain2.Age.mean()









# Cabin 的缺失值太多，从 Dataframe 中移除后，也不会影响预测的
# X_train.drop("Cabin", axis=1, inplace=True)
# 我们来看下乘客都在哪些站登船的
# S 表示：Southampton，英国南安普敦
# C 表示：Cherbourg-Octeville，法国瑟堡-奥克特维尔
# Q 表示：Queenstown，爱尔兰昆士敦
X_train.Embarked.value_counts()
# 登船情况
# fig5=plt.figure()
# fig5=sns.countplot(x='Embarked', data=X_train)
X_train['Embarked'].replace(np.nan, 'S', inplace=True)
# 数据集有一个缺失数据，我们把它找出来，然后附上中值
# X_train[np.isnan(X_train["Fare"])]
# 查询从 英国南安普敦 上船，级别是3的船票价格
pclass3_fares = X_train.query('Pclass == 3 & Embarked == "S"')['Fare']
# 先将空值填充为0
# pclass3_fares = pclass3_fares.replace(np.nan, 0)
# 然后取中值
# median_fare = np.median(pclass3_fares)
# 最后更新中值到缺失值的那处
# X_train.loc[X_train['PassengerId'] == 1044, 'Fare'] = median_fare
# 查看这个为乘客
X_train.loc[X_train['PassengerId'] == 1044]
X_train['Sex'].replace(['male', 'female'], [1,0], inplace=True)
X_train.isnull().sum()
#转化为计算机可以识别的
X_train['Embarked'].replace(['S', 'C','Q'], [1,2,3], inplace=True)
#数据集中name和ID重合了，删掉name
data=X_train.drop('Name',axis=1)
#也是重合了删掉船票编号
data.drop('Ticket',axis=1,inplace=True)
data.drop('PassengerId',axis=1,inplace=True)
data['2']=y_train1
corr = np.corrcoef(data,rowvar=False)
data.drop('2',axis=1,inplace=True)
# 填补缺失值的代码
# -------------------------------------

X = df.iloc[:, 2:]
y = df.iloc[:, 1]

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 特征缩放
mms = preprocessing.MinMaxScaler()
X_train = mms.fit_transform(X_train)
X_test = mms.transform(X_test)

# 主成分分析
pca = PCA(n_components=2)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)

# SVM模型
svm = SVC(probability=True)
svm.fit(X_train, y_train)

# 计算SVM ROC曲线的X轴坐标和Y轴坐标，混淆矩阵
y_score_svm = svm.predict_proba(X_test)[:, 1]
fpr_svm, tpr_svm, _ = roc_curve(y_test, y_score_svm)
roc_auc_svm = auc(fpr_svm, tpr_svm)

y_pred_svm = svm.predict(X_test)
cm_svm = confusion_matrix(y_test, y_pred_svm)

# 逻辑回归模型
lr = LogisticRegression()
lr.fit(X_train, y_train)

# 计算逻辑回归 ROC曲线的X轴坐标和Y轴坐标，混淆矩阵
y_score_lr = lr.predict_proba(X_test)[:, 1]
fpr_lr, tpr_lr, _ = roc_curve(y_test, y_score_lr)
roc_auc_lr = auc(fpr_lr, tpr_lr)

y_pred_lr = lr.predict(X_test)
cm_lr = confusion_matrix(y_test, y_pred_lr)

# 绘制ROC曲线图
plt.figure()
plt.plot(fpr_svm, tpr_svm, color='darkorange', lw=2, label='SVM (AUC = {:.2f})'.format(roc_auc_svm))
plt.plot(fpr_lr, tpr_lr, color='green', lw=2, label='Logistic Regression (AUC = {:.2f})'.format(roc_auc_lr))
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")

plt.show()
